/*****************************************************************************
   Major portions of this software are copyrighted by the Medical College
   of Wisconsin, 1994-2000, and are released under the Gnu General Public
   License, Version 2.  See the file README.Copyright for details.
******************************************************************************/
   
#include "afni.h"
#include "cylk.h"

#ifndef ALLOW_PLUGINS
#  error "Plugins not properly set up -- see machdep.h"
#endif

/***********************************************************************
  Plugin to project a cylinder through brain
************************************************************************/

static char * CYLK_main( PLUGIN_interface * );

static char helpstring[] =
   " Purpose: project a cylinder through brain volume.\n"
   " Inputs:\n"
   " Data - a dataset in the current session that exists in memory (not warp-on-demand)\n"
   " Rotate - whether to rotate axes along cylinder axis, a second angle of rotation (?)\n"
   " Region - whether to seed cyl. region at brightest voxel, pct. threshold for region\n"
   " Seed - coordinates from AFNI views where to seed cyl. region\n"
   "Authors -- L BLOY, Ben Heasly"
;

/***********************************************************************
   Set up the interface to the user
************************************************************************/


DEFINE_PLUGIN_PROTOTYPE

PLUGIN_interface * PLUGIN_init( int ncall )
{
   PLUGIN_interface * plint ;

   if( ncall > 0 ) return NULL ;  /* only one interface */

   // set titles and call point
   plint = PLUTO_new_interface("cylk" , 
							   "Project a cylinder through brain volume", 
							   helpstring,
							   PLUGIN_CALL_VIA_MENU, 
							   CYLK_main) ;

   PLUTO_add_hint( plint , "Project a cylinder" ) ;

   //PLUTO_set_sequence( plint , "A:newdset:copy" ) ;

   PLUTO_set_runlabels( plint , "Run+Keep" , "Run+Close" ) ;  /* 04 Nov 2003 */
	
	// option line for data
	PLUTO_add_option(plint, "Data", "Data", TRUE);
	PLUTO_add_dataset(plint, "Input", ANAT_ALL_MASK, FUNC_ALL_MASK, DIMEN_ALL_MASK|BRICK_ALLTYPE_MASK);

	// option line for projection
	PLUTO_add_option(plint, "Projection", "Projection", TRUE);
	PLUTO_add_number(plint, "do projection", 0, 1, 0, 1, FALSE);
	PLUTO_add_number(plint, "fit tolerance", 0, 100, 2, 3, TRUE);
	PLUTO_add_number(plint, "do rotation", 0, 1, 0, 1, FALSE);
		
	// option line for cylinder region detection
	PLUTO_add_option(plint, "Region", "Region", TRUE);
	PLUTO_add_number(plint, "seed at max voxel", 0, 1, 0, 1, FALSE);
	PLUTO_add_number(plint, "region thresh(%)", 0, 100, 0, 75, TRUE);
	
	// option line for cylinder region seed point
	PLUTO_add_option(plint, "Seed", "Seed", TRUE);
	PLUTO_add_number(plint, "saggital(x)", 0, 1e6, 0, 0, TRUE);
	PLUTO_add_number(plint, "coronal(y)", 0, 1e6, 0, 0, TRUE);
	PLUTO_add_number(plint, "axial(z)", 0, 1e6, 0, 0, TRUE);

	fprintf(stderr, "\n\n short %d float %d\n\n", sizeof(short), sizeof(float));
	
   return plint ;
}

/***************************************************************************
  Main routine for this plugin (will be called from AFNI).
****************************************************************************/

static char * CYLK_main( PLUGIN_interface * plint )
{
	MCW_idcode *idc ;
	THD_3dim_dataset *dset;
	float do_projection, fit_tolerance, do_rotation;
	float seed_at_max_voxel, region_thresh_pct;
	float saggital_x, coronal_y, axial_z;
	
	
	// get user inputs from plugin GUI
	if( plint == NULL )
		return "**********************\n"
		"CYLK_main:  NULL plugin interface\n"
		"**********************";

	// option line for data
	PLUTO_next_option(plint);
	idc  = PLUTO_get_idcode(plint);
	dset = PLUTO_find_dset(idc);
	if(dset==NULL)
		return "*****************************\n"
		"CYLK_main:  bad input dataset\n"
		"*****************************";

	// option line for projection
	PLUTO_next_option(plint);
	do_projection = PLUTO_get_number(plint);
	fit_tolerance = PLUTO_get_number(plint);
	do_rotation = PLUTO_get_number(plint);
	
	// option line for cylinder region detection
	PLUTO_next_option(plint);
	seed_at_max_voxel = PLUTO_get_number(plint);
	region_thresh_pct = PLUTO_get_number(plint);
	
	// option line for cylinder region seed point
	PLUTO_next_option(plint);
	saggital_x = PLUTO_get_number(plint);
	coronal_y = PLUTO_get_number(plint);
	axial_z = PLUTO_get_number(plint);
	
	// run cylk itself
	runCylk(dset,
			plint,
			(int) do_projection,
			fit_tolerance,
			(int) do_rotation, 
			(int) seed_at_max_voxel, 
			region_thresh_pct, 
			(int) saggital_x,
			(int) coronal_y, 
			(int) axial_z); 
	
	// AFNI should redraw dataset
	PLUTO_force_redisplay();
	PLUTO_fixup_names();

	return NULL ;
}
